package droid.frame.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shouhuobao.ui.R;

import droid.frame.ui.utils.Utils;
import droid.frame.ui.view.ProgressBarCircularIndeterminate;

/**
 * app加载进度框
 * 
 * @author coffee<br>
 *         2015-4-16下午4:02:34
 */
public class AppLoading extends Dialog {

	private ProgressBarCircularIndeterminate mProgressBar;
	/**
	 * 文本提示
	 */
	private TextView mMessage;

	public AppLoading(Activity context) {
		// 带主题样式
		super(context, R.style.app_dialog_theme);
		View mContentView = View.inflate(context, R.layout.app_loading, null);
		setContentView(mContentView);
		//
		this.mProgressBar = (ProgressBarCircularIndeterminate) mContentView.findViewById(R.id.app_loading_progressbar);
		this.mMessage = (TextView) mContentView.findViewById(R.id.app_loading_message);
		//
		setCanceledOnTouchOutside(false);
	}

	public void setMessage(String message) {
		if (mMessage == null) {
			return;
		}
		// 如果不需要显示文字
		if (TextUtils.isEmpty(message)) {
			ViewGroup.LayoutParams params = mProgressBar.getLayoutParams();
			params.width = Utils.dpToPx(48, getContext().getResources());
			params.height = Utils.dpToPx(48, getContext().getResources());
			mProgressBar.setLayoutParams(params);
			mMessage.setVisibility(View.GONE);
		} else {
			mMessage.setText(message);
		}
	}

	@Override
	public void dismiss() {
		try {
			if (isShowing()) {
				super.dismiss();
			}
		} catch (IllegalStateException e) {
			//
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
